home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q1082.dms / q1082.adf / src.lzh / Fig / save.c < prev    next >
C/C++ Source or Header  |  1991-07-18  |  6KB  |  251 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985, 1988 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : August 1985.
  7.  *    2nd revision : March 1988.
  8.  *
  9.  *    %W%    %G%
  10. */
  11. #include "fig.h"
  12. #include "resources.h"
  13. #include "func.h"
  14. #include "object.h"
  15. #include "const.h"
  16.  
  17. extern F_compound    objects;
  18.  
  19. extern int        figure_modified;
  20. extern int        errno;
  21.  
  22. extern             null_proc();
  23.  
  24. extern char        *sys_errlist[];
  25. extern int        sys_nerr, errno;
  26. extern int        num_object;
  27.  
  28. write_file(file_name, prompt)
  29. char    *file_name;
  30. int    prompt;
  31. {
  32.     FILE        *fp;
  33.     struct stat    file_status;
  34.     char        string[180];
  35.  
  36.     if (*file_name == 0) {
  37.         put_msg("No file.  Abort save operation.");
  38.         return(-1);
  39.         }
  40. #ifndef AMIGA
  41.     if (stat(file_name, &file_status) == 0) { /* file exists */
  42.         if (file_status.st_mode & S_IFDIR) {
  43.         put_msg("\"%s\" is a directory", file_name);
  44.         return(-1);
  45.         }
  46.         if (file_status.st_mode & S_IWRITE) { /* writing is permitted */
  47.         if (file_status.st_uid != geteuid()) {
  48.             put_msg("\"%s\" permission is denied", file_name);
  49.             return(-1);
  50.             }
  51.         else if (prompt) {
  52.             sprintf(string, "\"%s\" File exists. Please click the LEFT button to COMFIRM overwrite. To cancel, click the MIDDLE or RIGHT button.", file_name);
  53.             if (wmgr_confirm(canvas_swfd, string) != -1) {
  54.             put_msg("Cancel save");
  55.             return(-1);
  56.             }
  57.             }
  58.         }
  59.         else {
  60.         put_msg("\"%s\" File is read only", file_name);
  61.         return(-1);
  62.         }
  63.         }
  64.     else if (errno != ENOENT)
  65.         return(-1);  /* file does exist but stat fails */
  66. #endif
  67.     if ((fp = fopen(file_name, "w")) == NULL) {
  68.         blink_msg();
  69.         put_msg("Couldn't open file %s, %s", file_name, sys_errlist[errno]);
  70.         return(-1);
  71.         }
  72.     else {
  73.         figure_modified = 0;
  74.         num_object = 0;
  75.         write_objects(fp);
  76.         put_msg("%d objects saved in \"%s\"", num_object, file_name);
  77.         return(0);
  78.         }
  79.     }
  80.  
  81. write_objects(fp)
  82. FILE    *fp;
  83. {
  84.     extern char    file_header[];
  85.     F_arc        *a;
  86.     F_compound    *c;
  87.     F_ellipse    *e;
  88.     F_line        *l;
  89.     F_spline    *s;
  90.     F_text        *t;
  91.  
  92.     /*  Number 2 means that the origin (0,0) is at the upper
  93.     left corner of the screen (2nd quadrant)    */
  94.  
  95.     put_msg("Writing . . .");
  96.     fprintf(fp, "%s\n", file_header);
  97.     fprintf(fp, "%d %d\n", PIX_PER_INCH, 2);
  98.     for (a = objects.arcs; a != NULL; a = a-> next) {
  99.         num_object++;
  100.         write_arc(fp, a);
  101.         }
  102.     for (c = objects.compounds; c != NULL; c = c-> next) {
  103.         num_object++;
  104.         write_compound(fp, c);
  105.         }
  106.     for (e = objects.ellipses; e != NULL; e = e-> next) {
  107.         num_object++;
  108.         write_ellipse(fp, e);
  109.         }
  110.     for (l = objects.lines; l != NULL; l = l-> next) {
  111.         num_object++;
  112.         write_line(fp, l);
  113.         }
  114.     for (s = objects.splines; s != NULL; s = s-> next) {
  115.         num_object++;
  116.         write_spline(fp, s);
  117.         }
  118.     for (t = objects.texts; t != NULL; t = t-> next) {
  119.         num_object++;
  120.         write_text(fp, t);
  121.         }
  122.     fclose(fp);
  123.     }
  124.  
  125. write_arc(fp, a)
  126. FILE    *fp;
  127. F_arc    *a;
  128. {
  129.     F_arrow    *f, *b;
  130.  
  131.     fprintf(fp, "%d %d %d %d %d %d %d %d %.3f %d %d %d %.3f %.3f %d %d %d %d %d %d\n",
  132.         O_ARC, a->type, a->style, a->thickness, 
  133.         a->color, a->depth, a->pen, a->area_fill,
  134.         a->style_val, a->direction,
  135.         ((f = a->for_arrow) ? 1 : 0), ((b = a->back_arrow) ? 1 : 0),
  136.         a->center.x, a->center.y, 
  137.         a->point[0].x, a->point[0].y, 
  138.         a->point[1].x, a->point[1].y, 
  139.         a->point[2].x, a->point[2].y);
  140.     if (f)
  141.         fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", f->type, f->style,
  142.             f->thickness, f->wid, f->ht);
  143.     if (b)
  144.         fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", b->type, b->style,
  145.             b->thickness, b->wid, b->ht);
  146.     }
  147.  
  148. write_compound(fp, com)
  149. FILE        *fp;
  150. F_compound    *com;
  151. {
  152.     F_arc        *a;
  153.     F_compound    *c;
  154.     F_ellipse    *e;
  155.     F_line        *l;
  156.     F_spline    *s;
  157.     F_text        *t;
  158.  
  159.     fprintf(fp, "%d %d %d %d %d\n", O_COMPOUND, com->nwcorner.x,
  160.         com->nwcorner.y, com->secorner.x, com->secorner.y);
  161.     for (a = com->arcs; a != NULL; a = a-> next) write_arc(fp, a);
  162.     for (c = com->compounds; c != NULL; c = c-> next) write_compound(fp, c);
  163.     for (e = com->ellipses; e != NULL; e = e-> next) write_ellipse(fp, e);
  164.     for (l = com->lines; l != NULL; l = l-> next) write_line(fp, l);
  165.     for (s = com->splines; s != NULL; s = s-> next) write_spline(fp, s);
  166.     for (t = com->texts; t != NULL; t = t-> next) write_text(fp, t);
  167.     fprintf(fp, "%d\n", O_END_COMPOUND);
  168.     }
  169.  
  170. write_ellipse(fp, e)
  171. FILE        *fp;
  172. F_ellipse    *e;
  173. {
  174.     fprintf(fp, "%d %d %d %d %d %d %d %d %.3f %d %.3f %d %d %d %d %d %d %d %d\n",
  175.         O_ELLIPSE, e->type, e->style, e->thickness, 
  176.         e->color, e->depth, e->pen, e->area_fill,
  177.         e->style_val, e->direction, e->angle,
  178.         e->center.x, e->center.y, 
  179.         e->radiuses.x, e->radiuses.y, 
  180.         e->start.x, e->start.y, 
  181.         e->end.x, e->end.y);
  182.     }
  183.  
  184. write_line(fp, l)
  185. FILE    *fp;
  186. F_line    *l;
  187. {
  188.     F_point    *p;
  189.     F_arrow    *f, *b;
  190.  
  191.     fprintf(fp, "%d %d %d %d %d %d %d %d %.3f %d %d\n",
  192.         O_POLYLINE, l->type, l->style, l->thickness,
  193.         l->color, l->depth, l->pen, l->area_fill, l->style_val, 
  194.         ((f = l->for_arrow) ? 1 : 0), ((b = l->back_arrow) ? 1 : 0));
  195.     if (f)
  196.         fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", f->type, f->style,
  197.             f->thickness, f->wid, f->ht);
  198.     if (b)
  199.         fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", b->type, b->style,
  200.             b->thickness, b->wid, b->ht);
  201.     fprintf(fp, "\t");
  202.     for (p = l->points; p!= NULL; p = p->next) {
  203.         fprintf(fp, " %d %d", p->x, p->y);
  204.         };
  205.     fprintf(fp, " 9999 9999\n");
  206.     }
  207.  
  208. write_spline(fp, s)
  209. FILE        *fp;
  210. F_spline    *s;
  211. {
  212.     F_control    *cp;
  213.     F_point        *p;
  214.     F_arrow        *f, *b;
  215.  
  216.     fprintf(fp, "%d %d %d %d %d %d %d %d %.3f %d %d\n",
  217.         O_SPLINE, s->type, s->style, s->thickness,
  218.         s->color, s->depth, s->pen, s->area_fill, s->style_val,
  219.         ((f = s->for_arrow) ? 1 : 0), ((b = s->back_arrow) ? 1 : 0));
  220.     if (f)
  221.         fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", f->type, f->style,
  222.             f->thickness, f->wid, f->ht);
  223.     if (b)
  224.         fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", b->type, b->style,
  225.             b->thickness, b->wid, b->ht);
  226.     fprintf(fp, "\t");
  227.     for (p = s->points; p != NULL; p = p->next) {
  228.         fprintf(fp, " %d %d", p->x, p->y);
  229.         };
  230.     fprintf(fp, " 9999 9999\n");  /* terminating code  */
  231.  
  232.     if (s->controls == NULL) return;
  233.     fprintf(fp, "\t");
  234.     for (cp = s->controls; cp != NULL; cp = cp->next) {
  235.         fprintf(fp, " %.3f %.3f %.3f %.3f",
  236.             cp->lx, cp->ly, cp->rx, cp->ry);
  237.         };
  238.     fprintf(fp, "\n");
  239.     }
  240.  
  241. write_text(fp, t)
  242. FILE    *fp;
  243. F_text    *t;
  244. {
  245.     fprintf(fp, "%d %d %d %d %d %d %d %.3f %d %d %d %d %d %s\1\n", 
  246.         O_TEXT, t->type, t->font, t->size, t->pen,
  247.         t->color, t->depth, t->angle,
  248.         t->style, t->height, t->length, 
  249.         t->base_x, t->base_y, t->cstring);
  250.     }
  251.